﻿// ECMAScript 5 strict mode
"use strict";

assert2(cr, "cr namespace not created");
assert2(cr.plugins_, "cr.plugins_ not created");


/////////////////////////////////////
// Plugin class
// *** CHANGE THE PLUGIN ID HERE *** - must match the "id" property in edittime.js
//          vvvvvvvv
cr.plugins_.CKjsMessage = function(runtime)
{
	this.runtime = runtime;
};

(function ()
{
	/////////////////////////////////////
	// *** CHANGE THE PLUGIN ID HERE *** - must match the "id" property in edittime.js
	//                            vvvvvvvv
	var pluginProto = cr.plugins_.CKjsMessage.prototype;
		
	/////////////////////////////////////
	// Object type class
	pluginProto.Type = function(plugin)
	{
		this.plugin = plugin;
		this.runtime = plugin.runtime;
	};

	var typeProto = pluginProto.Type.prototype;

	// called on startup for each object type
	typeProto.onCreate = function()
	{
	};

	/////////////////////////////////////
	// Instance class
	pluginProto.Instance = function(type)
	{
		this.type = type;
		this.runtime = type.runtime;
		
		// any other properties you need, e.g...
		// this.myValue = 0;
	};
	
	var instanceProto = pluginProto.Instance.prototype;


	// called whenever an instance is created
	instanceProto.onCreate = function()
	{
		// note the object is sealed after this call; ensure any properties you'll ever need are set on the object
		// e.g...
		// this.myValue = 0;
		var self = this;
		this.Message = "";
		this.Command = "";
		this.Values = new Array;

		
		if (!this.runtime.isDomFree){
			jQuery(document).on('CKjsMessageSend',
				function(e,Message) {
					self.ReceiveMessage(Message);
				}
			);
			jQuery(document).on('CKjsMessageSeparatorQ',
				function(){
					jQuery(document).trigger('CKjsMessageSeparatorA',self.properties[0]);
				}
			);
		}
		
	};

	instanceProto.ReceiveMessage = function (Message) {
		this.Message = Message;
		var mSplit = Message.split(this.properties[0]);
		this.Command = mSplit[0];
		this.Values.length = 0;
		for(var v=1;v<mSplit.length;v++){
			this.Values[this.Values.length] = mSplit[v];
		}
		this.runtime.trigger(cr.plugins_.CKjsMessage.prototype.cnds.MessageReceived, this);
		this.runtime.trigger(cr.plugins_.CKjsMessage.prototype.cnds.CommandReceived, this);
	};

	
	// called whenever an instance is destroyed
	// note the runtime may keep the object after this call for recycling; be sure
	// to release/recycle/reset any references to other objects in this function.
	instanceProto.onDestroy = function ()
	{
	};
	
	// called when saving the full state of the game
	instanceProto.saveToJSON = function ()
	{
		// return a Javascript object containing information about your object's state
		// note you MUST use double-quote syntax (e.g. "property": value) to prevent
		// Closure Compiler renaming and breaking the save format
		return {
			// e.g.
			//"myValue": this.myValue
		};
	};
	
	// called when loading the full state of the game
	instanceProto.loadFromJSON = function (o)
	{
		// load from the state previously saved by saveToJSON
		// 'o' provides the same object that you saved, e.g.
		// this.myValue = o["myValue"];
		// note you MUST use double-quote syntax (e.g. o["property"]) to prevent
		// Closure Compiler renaming and breaking the save format
	};
	
	// only called if a layout object - draw to a canvas 2D context
	instanceProto.draw = function(ctx)
	{
	};
	
	// only called if a layout object in WebGL mode - draw to the WebGL context
	// 'glw' is not a WebGL context, it's a wrapper - you can find its methods in GLWrap.js in the install
	// directory or just copy what other plugins do.
	instanceProto.drawGL = function (glw)
	{
	};
	
	// The comments around these functions ensure they are removed when exporting, since the
	// debugger code is no longer relevant after publishing.
	/**BEGIN-PREVIEWONLY**/
	instanceProto.getDebuggerValues = function (propsections)
	{
		// Append to propsections any debugger sections you want to appear.
		// Each section is an object with two members: "title" and "properties".
		// "properties" is an array of individual debugger properties to display
		// with their name and value, and some other optional settings.
		propsections.push({
			"title": "My debugger section",
			"properties": [
				// Each property entry can use the following values:
				// "name" (required): name of the property (must be unique within this section)
				// "value" (required): a boolean, number or string for the value
				// "html" (optional, default false): set to true to interpret the name and value
				//									 as HTML strings rather than simple plain text
				// "readonly" (optional, default false): set to true to disable editing the property
				
				// Example:
				// {"name": "My property", "value": this.myValue}
			]
		});
	};
	
	instanceProto.onDebugValueEdited = function (header, name, value)
	{
		// Called when a non-readonly property has been edited in the debugger. Usually you only
		// will need 'name' (the property name) and 'value', but you can also use 'header' (the
		// header title for the section) to distinguish properties with the same name.
		if (name === "My property")
			this.myProperty = value;
	};
	/**END-PREVIEWONLY**/

	//////////////////////////////////////
	// Conditions
	function Cnds() {};

	Cnds.prototype.MessageReceived = function ()
	{
		return true;
	};

	Cnds.prototype.CommandReceived = function (Command){
		return this.Command == Command;
	};
	
	
	// ... other conditions here ...
	
	pluginProto.cnds = new Cnds();
	
	//////////////////////////////////////
	// Actions
	function Acts() {};

	Acts.prototype.SendMessage = function(Message){
		//Trigger the Receive event with the message
		if (!this.runtime.isDomFree){
			jQuery(document).trigger('CKjsMessageReceive',Message);
		}
	};
	
	
	// ... other actions here ...
	
	pluginProto.acts = new Acts();
	
	//////////////////////////////////////
	// Expressions
	function Exps() {};
	
	Exps.prototype.MessageRaw = function(ret){
		ret.set_string(this.Message);
	};

	Exps.prototype.ValueCount = function(ret){
		ret.set_int(this.Values.length);
	};

	Exps.prototype.Command = function(ret){
		ret.set_string(this.Command);
	};

	Exps.prototype.Value = function(ret, index){
		if(this.Values.length > index && index >= 0){
			ret.set_string(this.Values[index]);
		} else {
			ret.set_string('');
		}
	};
	
	Exps.prototype.Separator = function(ret){
		ret.set_string(this.properties[0]);
	};
	
	
	// ... other expressions here ...
	
	pluginProto.exps = new Exps();

}());